package com.xwd.listener;

import com.mysql.cj.jdbc.AbandonedConnectionCleanupThread;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.sql.DriverManager;

public class ApplicationContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("ApplicationContext start...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("ApplicationContext stop...");
        try {
            while (DriverManager.getDrivers().hasMoreElements()){
                DriverManager.deregisterDriver(DriverManager.getDrivers().nextElement());
            }
            System.out.println("JDBC Driver closed...");
            AbandonedConnectionCleanupThread.checkedShutdown();
            System.out.println("clean Thread success!");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
